﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace AsteroidWar
{
    class QuitMenu
    {

        List<Button> buttonList = new List<Button>();
        ContentManager contentManager;
        public int quit;

        MouseState previousMouseState;
        MouseState currentMouseState;
        public QuitMenu(ContentManager contentManager)
        {
            this.quit = 0;
            this.contentManager = contentManager;
        }

        public void Init()
        {
            Texture2D confirm = contentManager.Load<Texture2D>(@"Textures\Buttons\confirm");
            Texture2D ResumetGameButton = contentManager.Load<Texture2D>(@"Textures\Buttons\resume");
            Texture2D QuittGameButton = contentManager.Load<Texture2D>(@"Textures\Buttons\quit");

            Button confirmbtn = new Button("Confirm", confirm, new Vector2(250, 315));
            Button resumebtn = new Button("Resume", ResumetGameButton, new Vector2(100, 400));
            Button quitbtn = new Button("Quit", QuittGameButton, new Vector2(400, 400));

            buttonList.Clear();
            buttonList.Add(confirmbtn);
            buttonList.Add(resumebtn);
            buttonList.Add(quitbtn);
        }

        protected void ChangeMainState(MouseState mouseState)
        {
            if (checkMouseState(mouseState))
            {
                foreach (Button button in buttonList)
                {
                    button.Update(mouseState);
                    if (button.isClicked)
                    {
                        switch (button.name)
                        {
                            case "Confirm":
                                break;
                            case "Resume":
                                quit = 2;
                                break;
                            case "Quit":
                                quit = 1;
                                break;
                        }
                        break;
                    }
                }
            }
        }



        public void Update(GameTime gametime, MouseState mousetate)
        {
              Init();
              ChangeMainState(mousetate);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Button button in buttonList)
            {
                button.Draw(spriteBatch);
            }
        }

        private bool checkMouseState(MouseState mouseState)
        {
            currentMouseState = mouseState;
            bool result = true;
            if (currentMouseState.LeftButton == ButtonState.Pressed && currentMouseState != previousMouseState)
            {
                //Allow change state
                result = true;
            }
            if (currentMouseState.LeftButton == ButtonState.Pressed && currentMouseState == previousMouseState)
            {
                //Deny change state
                result = false;
            }
            previousMouseState = currentMouseState;
            return result;
        }


    }

}
